package ch.epfl.lara.pm.util;

import ch.epfl.lara.pm.ast._
import ch.epfl.lara.pm.transformation._
import ch.epfl.lara.pm.parser.NScalaParser

import java.io.File

trait Tester extends NScalaParser with Transform {

  
  protected def parseAndTransform(filename: String): Tree = transform(parseOnly(filename))
  
  protected def parseOnly(filename: String): Tree = test(parseFile(filename))
  
  override def parseFile(filename: String): ParseResult[Tree] = {
    if(fileExists(filename)) {
      println("Executing source: "+filename+"\n")
      super.parseFile(filename)
    } else { 
      error("File "+filename+" doesn't exists!")
    }
  }
  
  
  protected def fileExists(filename: String): Boolean = new File(filename).isFile
  
  protected def test(term: ParseResult[Tree]): Tree = {
    term match {
      case Success(term, _) => term
      case Failure(msg, remainder) => error("Failure: "+msg+"\n"+"Remainder: \n"+remainder.pos.longString);
      case Error(msg, remainder) => error("Error: "+msg+"\n"+"Remainder: \n"+remainder.pos.longString);
    }
  }
}
